package com.ewhale.feitengguest.ui.shop.mine

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.ShopAuthDto
import com.ewhale.feitengguest.presenter.shop.mine.ShopAuthPresenter
import com.ewhale.feitengguest.view.shop.mine.ShopAuthView
import com.orhanobut.hawk.Hawk
import com.simga.library.activity.MBaseActivity
import com.simga.library.utils.HawkKey
import kotlinx.android.synthetic.main.activity_shop_auth.*

class ShopAuthActivity : MBaseActivity<ShopAuthPresenter>(), ShopAuthView
        /**EasyPermissions.PermissionCallbacks*/ {
    override fun getViewId(): Int {
        return R.layout.activity_shop_auth
    }

    override fun isUseBaseTitleBar(): Boolean {
        return true
    }

//    private var chooseType = 1
//    private var uploadImage: Int = 0
//    private var idCardFront = ""
//    private var idCardRear = ""
    private var anthDto: ShopAuthDto? = null
//    private val mPhotoDialog by lazy { SelectPhotoDialog(mContext) }
    override fun init(savedInstanceState: Bundle?) {
        setTitle("商家认证")
        presenter.getAuthInfo()
    }

    override fun showAuthInfo(shopAuth: ShopAuthDto) {
        this.anthDto = shopAuth
        topScheduleStyle(shopAuth.status, shopAuth.refuseReason)
        if (shopAuth.status == 3) {
            ll_dot.visibility = View.GONE
            ll_stepHint.visibility = View.GONE
            et_name.apply {
                isEnabled = false
                setText(shopAuth.name)
            }
            et_phone.apply {
                isEnabled = false
                setText(shopAuth.phone)
            }
            et_idcard.apply {
                isEnabled = false
                setText(shopAuth.certificateNumber)
            }
            et_address.apply {
                isEnabled = false
                setText(shopAuth.address)
            }
//            iv_cardFront.isEnabled = false
//            iv_cardRear.isEnabled = false
//            val shopType = Hawk.get(HawkKey.LOGIN_SHOP_TYPE, 1)
//            if (shopType == 2) {
//                val imgs = shopAuth.businessImg.split(",")
//                GlideUtil.loadPicture(imgs[0], iv_cardFront, R.mipmap.d1_btn_camera)
//                GlideUtil.loadPicture(imgs[1], iv_cardRear, R.mipmap.d1_btn_camera)
//            } else {
//                GlideUtil.loadPicture(shopAuth.certificateFrontImg, iv_cardFront, R.mipmap.d1_btn_camera)
//                GlideUtil.loadPicture(shopAuth.certificateBackImg, iv_cardRear, R.mipmap.d1_btn_camera)
//            }
        }
    }

    override fun initListener() {
        super.initListener()
        btn_next.setOnClickListener {
            val shopType = Hawk.get(HawkKey.LOGIN_SHOP_TYPE, 1)
            when (btn_next.text) {
                "下一步" -> {
                    if (et_name.toString().isEmpty() || et_phone.toString().isEmpty() || et_address.toString().isEmpty()) {
                        showToast("请完善信息")
                        return@setOnClickListener
                    }
                    if (shopType != 2) {
                        if (et_idcard.toString().isEmpty()) {
                            showToast("请完善信息")
                            return@setOnClickListener
                        }
                    }
                    btn_next.visibility = View.VISIBLE
                    ll_auth_first.visibility = View.GONE
                    tv_reson.visibility = View.GONE
//                    cl_auth_second.visibility = View.VISIBLE

                    btn_next.text = "提交"
                    line_one.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color))
                    tv_second.setTextColor(ContextCompat.getColor(mContext, R.color.main_color))
//                    cirle_one.setBackgroundResource(R.drawable.draw_cirle_main_color)
                }
                "提交" -> {
//                    if (idCardFront.isEmpty() || idCardRear.isEmpty()) {
//                        showToast("请上传证件照")
//                        return@setOnClickListener
//                    }
                    presenter!!.commitAuInfo(shopType, if (anthDto == null) 1 else 2, et_name.text.toString(), et_phone.text.toString(), et_address.text.toString(),
                            et_idcard.text.toString())
                }
                "重新编辑" -> {
                    btn_next.visibility = View.VISIBLE
                    ll_auth_first.visibility = View.VISIBLE
                    tv_reson.visibility = View.GONE
//                    cl_auth_second.visibility = View.GONE

                    btn_next.text = "下一步"
                    line_one.setBackgroundColor(ContextCompat.getColor(mContext, R.color.text_999999))
                    tv_second.setTextColor(ContextCompat.getColor(mContext, R.color.text_999999))
//                    cirle_one.setBackgroundResource(R.drawable.draw_cirle_999999)
//                    line_two.setBackgroundColor(ContextCompat.getColor(mContext, R.color.text_999999))
                    tv_third.setTextColor(ContextCompat.getColor(mContext, R.color.text_999999))
                    cirle_two.setBackgroundResource(R.drawable.draw_cirle_999999)
                }
            }
        }
//        iv_cardFront.setOnClickListener {
//            uploadImage = 1
//            mPhotoDialog.show()
//        }
//        iv_cardRear.setOnClickListener {
//            uploadImage = 2
//            mPhotoDialog.show()
//        }
//        mPhotoDialog.setOnClickItem(object : SelectPhotoDialog.onClickItem {
//            override fun onOneClick() {
//                chooseType = 1
//                requestPermissionsMain()
//            }
//            override fun onTwoClick() {
//                chooseType = 2
//                requestPermissionsMain()
//            }
//        })
//        iv_deleteCardFront.setOnClickListener {
//            idCardFront = ""
//            iv_cardFront.setImageResource(R.mipmap.d1_btn_camera)
//            showDeleteImage()
//        }
//        iv_deleteCardRear.setOnClickListener {
//            idCardRear = ""
//            iv_cardRear.setImageResource(R.mipmap.d1_btn_camera)
//            showDeleteImage()
//        }
    }

    override fun commitSuccess() {
        btn_next.visibility = View.GONE
        ll_auth_first.visibility = View.GONE
        tv_reson.visibility = View.VISIBLE
//        cl_auth_second.visibility = View.GONE

//        line_two.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color))
        tv_third.setTextColor(ContextCompat.getColor(mContext, R.color.main_color))
        cirle_two.setBackgroundResource(R.drawable.draw_cirle_main_color)
        tv_reson.text = "资料已提交\n平台将会在3个工作日内审核"
    }
//    @AfterPermissionGranted(REQUEST_CODE_PERMISSION_2)
//    private fun requestPermissionsMain() {
//        val perms = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
//        if (EasyPermissions.hasPermissions(mContext, *perms)) {
//            if (chooseType == 1) {
//                PictureUtils.openCameraRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE)
//            } else {
//                PictureUtils.openAluamOneRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE)
//            }
//        } else {
//            EasyPermissions.requestPermissions(this, "申请获取相关权限",
//                    REQUEST_CODE_PERMISSION_2, *perms)
//        }
//    }

//    override fun onPermissionsGranted(requestCode: Int, perms: List<String>) {
//        if (chooseType == 1) {
//            PictureUtils.openCameraRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE)
//        } else {
//            PictureUtils.openAluamOneRectangle(this, REQUEST_CODE_CHOOSE_COMMENT_IMAGE)
//        }
//    }

//    override fun onPermissionsDenied(requestCode: Int, perms: List<String>) {
//        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
//            AppSettingsDialog.Builder(this).setTitle("权限申请")
//                    .setRationale("申请获取相关权限")
//                    .setPositiveButton(getString(R.string.comfir))
//                    .setNegativeButton(getString(R.string.cancle))
//                    .build()
//                    .show()
//        }
//    }
//
//    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
//    }

//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        if (resultCode != Activity.RESULT_OK) {
//            return
//        }
//        if (requestCode == REQUEST_CODE_CHOOSE_COMMENT_IMAGE) {
//            val images = ArrayList<String>()
//            images.add(PictureSelector.obtainMultipleResult(data)[0].cutPath)
//            showLoading()
//            QiniuUtils.uploadImage(images) { keys ->
//                dismissLoading()
//                when (uploadImage) {
//                    1 -> {
//                        idCardFront = keys[0]
//                        GlideUtil.loadPicture(idCardFront, iv_cardFront, R.mipmap.d1_btn_camera)
//                    }
//                    2 -> {
//                        idCardRear = keys[0]
//                        GlideUtil.loadPicture(idCardRear, iv_cardRear, R.mipmap.d1_btn_camera)
//                    }
//                }
//                showDeleteImage()
//                PictureUtils.clearCache(mContext)
//            }
//        }
//    }

//    private fun showDeleteImage() {
//        if (CheckUtil.isNull(idCardFront)) {
//            iv_deleteCardFront.visibility = View.GONE
//        } else {
//            iv_deleteCardFront.visibility = View.VISIBLE
//        }
//        if (CheckUtil.isNull(idCardRear)) {
//            iv_deleteCardRear.visibility = View.GONE
//        } else {
//            iv_deleteCardRear.visibility = View.VISIBLE
//        }
//    }

    @SuppressLint("SetTextI18n")
    private fun topScheduleStyle(status: Int, refuseReason: String) {
        val shopType = Hawk.get(HawkKey.LOGIN_SHOP_TYPE, 1)
        if (shopType == 2) {//公司
            tv_first.text = "输入公司信息"
//            tv_up_idcard.text = "营业执照"
            tv_name.text = "公司名称"
            tv_phone.text = "联系方式"
            tv_address.text = "公司地址　"
            cll_idcard.visibility = View.GONE
        } else {//个人
            tv_first.text = "输入身份信息"
//            tv_up_idcard.text = "请上传身份证正反面"
            tv_name.text = "真实姓名"
            cll_idcard.visibility = View.VISIBLE
            tv_phone.text = "手机号　"
            tv_address.text = "身份证地址"
        }
        if (status == 0) {
            ll_auth_first.visibility = View.VISIBLE
            btn_next.visibility = View.VISIBLE
            tv_reson.visibility = View.GONE
//            cl_auth_second.visibility = View.GONE
        } else if (status == 1 || status == 2) {
            line_one.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color))
            tv_second.setTextColor(ContextCompat.getColor(mContext, R.color.main_color))
//            cirle_one.setBackgroundResource(R.drawable.draw_cirle_main_color)
//            line_two.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color))
            tv_third.setTextColor(ContextCompat.getColor(mContext, R.color.main_color))
            cirle_two.setBackgroundResource(R.drawable.draw_cirle_main_color)

            ll_auth_first.visibility = View.GONE
            tv_reson.visibility = View.VISIBLE
            if (status == 2) {
                btn_next.text = "重新编辑"
                btn_next.visibility = View.VISIBLE
                tv_reson.text = "资料被驳回，请重新编辑\n驳回理由：$refuseReason"
            } else {
                tv_reson.text = "资料已提交\n平台将会在3个工作日内审核"
                btn_next.visibility = View.GONE
            }
//            cl_auth_second.visibility = View.GONE
        } else {
            btn_next.visibility = View.GONE
            ll_auth_first.visibility = View.VISIBLE
            tv_reson.visibility = View.GONE
//            cl_auth_second.visibility = View.VISIBLE
        }
    }

    override fun showProLoading() {
        showLoading()
    }

    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String?, message: String?) {
        if (postCode == 1 && code == "null") {//当返回的是null的时候，就是未认证
            topScheduleStyle(0, "")
        } else {
            showErrorMsg(code, message)
        }
    }

    companion object {
        const val REQUEST_CODE_PERMISSION_2 = 10002
        const val REQUEST_CODE_CHOOSE_COMMENT_IMAGE = 1000
        fun open(mContext: MBaseActivity<*>) {
            mContext.startActivity(null, ShopAuthActivity::class.java)
        }
    }
}
